<template>
  <div>
    <a-card :bordered='false'>
      <f-search-form :more='false' @searchHandler='searchHandler'>
        <f-search-form-item label='年月' type='month' name='financeMonth' style='width:220px' />
        <f-search-form-item label='供货方' ref='supplier' name='supplierId' type='select' :data='api.supplier.list' valueKey='supplierId'
                            label-key='supplierName' />
      </f-search-form>
      <a-tabs @change='changeHandler'>
        <a-tab-pane key='2' tab='本期合计'>
        </a-tab-pane>
        <a-tab-pane key='1' tab='本期累计'>
        </a-tab-pane>
      </a-tabs>
      <f-table ref='table' rowKey='sfinanceId' :scroll='{x:true}' :columns='columns' :multiple='false' :operator='operator'
               :params='queryParam' :api='api.sfinance'>
        <span slot='action' slot-scope='text, record'>
          <template>
            <a @click='showDetailsList(record)'>明细</a>
            <a-divider type='vertical' />
            <a @click='$refs.drawer.showDrawer(record)' v-if='record.importData==1'>编辑</a>
            <a-divider type='vertical' v-if='record.importData==1' />
             <a href='javascript:;' @click='downloadData(record)'>导出</a>
             <a-divider type='vertical' />
          </template>
          <a-dropdown>
            <a class='ant-dropdown-link'>更多 <a-icon type='down' /></a>
            <a-menu slot='overlay'>
              <a-menu-item >
                  <a @click='syncData(record)' v-if='record.importData==0&&record.allowInit==1'>初始化</a>
              </a-menu-item>
                <a-menu-item >
                  <a @click="$refs.drawer.showDrawer(record,'form3')" >差异修复</a>
              </a-menu-item>
               <a-menu-item v-if="$auth('table.delete')">
                <a href='javascript:;' @click='$refs.table.del(record)'>删除</a>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </f-table>
      <finance-drawer ref='drawer'></finance-drawer>
      <upload-finance-modal ref='uploadModal' :action='api.sfinance.importData' @change='uploadEnd()'></upload-finance-modal>
      <init-finance-modal ref='initModal'></init-finance-modal>
    </a-card>
  </div>
</template>
<script>
import financeDrawer from "./finance_drawer.vue"
import uploadFinanceModal from "./uploadFinanceModal.vue"
import initFinanceModal from "./initFinanceModal.vue"
import api from "./js/api"

export default {
  name: "TableList",
  components: {
    financeDrawer,
    uploadFinanceModal,
    initFinanceModal
  },
  data() {
    let me = this
    //操作按钮
    let operator = []
    operator.push({
      name: "导入上期数据",
      icon: "import",
      that: this,
      handler: function(table, that) {
        that.$refs.uploadModal.open()
      }
    })
    operator.push({
      name: "创建对账表",
      that: this,
      handler: function(table, that) {
        that.$refs.initModal.open()
      }
    })
    return {
      api: Object.assign(api, this.api),
      //列表初始化查询参数
      queryParam: {},
      financeType: 2,
      // 表头
      columns: this.getColumns(),
      operator: operator
    }
  },
  created() {
  },
  watch: {
    financeType(value) {
      this.columns = this.getColumns()
      // if (value == 1) {
      //   this.operator = []
      // } else {
      //
      //   this.operator = [{
      //     name: "创建对账表",
      //     that: this,
      //     handler: function(table, that) {
      //       that.$refs.initModal.open()
      //     }
      //   }]
      // }
    }
  },
  methods: {
    showDetailsList(record) {
      this.$refs["drawer"].showDrawer(record, "form2")
    },
    getColumns() {
      let columns = [
        {
          title: "序号",
          width: "60",
          align: "center",
          customRender: (text, record, index) => index + 1
        },
        {
          title: "账期",
          align: "center",
          dataIndex: "financeMonth"
        },
        {
          title: "供货方",
          align: "center",
          dataIndex: "supplierName"
        },
        {
          title: "累计发货金额",
          align: "center",
          dataIndex: "saleAmount"
        },
        {
          title: "累计客户回款",
          align: "center",
          dataIndex: "returnAmount"
        },
        {
          title: "累计客户欠款",
          align: "center",
          dataIndex: "debtAmount"
        },
        {
          title: "累计成本金额",
          align: "center",
          dataIndex: "costAmount"
        },
        {
          title: "累计垫付款",
          align: "center",
          dataIndex: "supplierBondAmount"
        },
        {
          title: "累计高开税金",
          align: "center",
          dataIndex: "supplierGapUpTaxesAmount"
        },
        {
          title: "累计应返金额",
          align: "center",
          dataIndex: "supplierRetableAmount"
        },
        {
          title: "累计已返金额",
          align: "center",
          dataIndex: "supplierRealAmount"
        },
        {
          title: "累计应收总金额",
          align: "center",
          dataIndex: "supplierAmount"
        },
        {
          title: "操作",
          align: "center",
          dataIndex: "action",
          width: "150px",
          fixed: "right",
          scopedSlots: { customRender: "action" }
        }
      ]
      if (this.financeType == 1) {
        columns.pop()
      }
      return columns

    },
    downloadData(record) {
      this.$message.loading("数据导出中..", 0)
      let that = this
      that.http.exportExcel(that, {
        url: api.sfinance.exportData,
        data: {
          sfinanceId: record.sfinanceId
        },
        success: function(me, data) {

        }
      })
    },

    syncData(record) {
      this.http.confirm(this, {
        content: "确定初始化该期数据吗?",
        url: api.sfinance.syncData,
        data: {
          sfinanceId: record.sfinanceId
        },
        success: function(me, data) {
          me.$refs.table.refresh(false)
        }
      })
    },
    uploadEnd() {
      this.$refs.uploadModal.close()
      this.$message.success("导入成功")
      this.$refs.table.refresh(false, this.queryParam)
    },
    changeHandler(financeType) {
      this.financeType = financeType
      this.$refs.table.refresh(true, { financeType: this.financeType })
    }
  }
}
</script>
